package product;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MemberHolyEditor extends JInternalFrame {
	
	Connection conn;
	ProductMain3 pm;
	PreparedStatement pstmt;
	DefaultComboBoxModel cmodel = new DefaultComboBoxModel<>();
	ResultSet rs;
	
	Calendar c;
	String yy="";
	String mm="";
	String dd="";
	
	private JComboBox comboBox;
	private JLabel label;
	private JComboBox comboBox_1;
	private JLabel label_1;
	private JComboBox combodd;
	private JLabel label_2;
	private JButton btnNewButton;
	
	String[] y = {"","2014", "2015", "2016", "2017"};
	String[] m = {"","01","02","03","04","05","06","07","08","09","10","11","12"};
	String[] d = {"", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};


	
	public MemberHolyEditor(Connection conn, ProductMain3 pm) {
		super("관리자 무급휴가설정 모드",true,true,true,false);
		this.conn=conn;
		this.pm=pm;
		
		setBounds(100, 100, 450, 80);
		getContentPane().setLayout(null);
		getContentPane().add(getComboBox());
		getContentPane().add(getLabel());
		getContentPane().add(getComboBox_1());
		getContentPane().add(getLabel_1());
		getContentPane().add(getCombodd());
		getContentPane().add(getLabel_2());
		getContentPane().add(getBtnNewButton());
		
		defaultdate();

	}
	public void defaultdate(){
		c = Calendar.getInstance();

		int year = c.get ( c.YEAR );
		int month = c.get ( c.MONTH ) + 1 ;
		int nal = c.get(c.DATE);
		
		
		String Year = Integer.toString(year);
		String Month = "";
		if(month<10){
			Month = "0"+month;
		}else{
			Month = Integer.toString(month);
		}
		String Nal = Integer.toString(nal);
		
		comboBox.setSelectedItem(Year);
		comboBox_1.setSelectedItem(Month);
		combodd.setSelectedItem(Nal);
		
	}
	public void memberholyeditor(){
		try{
			String nal = yy + "/" + mm +"/" + dd;
			if(!yy.equals("") && !mm.equals("") && !dd.equals("")){
				int yn = JOptionPane.showConfirmDialog(null, "무급휴가를 적용하시겠습니까?");
				if(yn == JOptionPane.YES_OPTION){
					
					// nal 에 휴가가 있는 사원들을 무급으로 바꾸는 쿼리
					String sql = "update holydata set holytype=?, info=? where holydate = any(select holydate from holydata where holydate=?)";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, "0");
					pstmt.setString(2, "회사내규");
					pstmt.setString(3, nal);
					pstmt.executeQuery();
//					
					System.out.println("업데이트완료");

					String sql2 = "select memberid from memberdata where memberid not in (select memberid from holydata where holydate=?) and working='1'";
					pstmt = conn.prepareStatement(sql2);
					pstmt.setString(1, nal);
					rs = pstmt.executeQuery();
					while(rs.next()){
						String id = rs.getString("memberid");
						System.out.println(id);
						
						String sql3 = "insert into holydata(holyno,memberid, holydate, holytype, info) values(holy_seq_no.nextval,?,?,?,?)";
						pstmt = conn.prepareStatement(sql3);
						pstmt.setString(1, id);
						pstmt.setString(2, nal);
						pstmt.setString(3, "0");
						pstmt.setString(4, "회사내규");
						pstmt.executeQuery();
						
					}
					
					JOptionPane.showMessageDialog(null, "적용되었습니다");
					
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox(y);
			comboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					

					if(e.getStateChange() == e.SELECTED){ // 콤보박스 값이 선택되었을경우
						yy = (String)(e.getItem());
					}
					
				}
			});
			comboBox.setBounds(10, 10, 69, 24);
		} 
		return comboBox;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("년");
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setBounds(85, 10, 35, 24);
		}
		return label;
	}
	private JComboBox getComboBox_1() {
		if (comboBox_1 == null) {
			comboBox_1 = new JComboBox(m);
			comboBox_1.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					
					if(e.getStateChange() == e.SELECTED){
						cmodel.removeAllElements();
						mm = (String)(e.getItem());
						// 4,6,9,11월은 30일까지 나옴
						if(mm.equals("04") || mm.equals("06") || mm.equals("09") || mm.equals("11")){
							cmodel.addElement("");
							for(int i=1;i<=30;i++){
								if(i<10){
									cmodel.addElement("0"+Integer.toString(i));
								}else{
									cmodel.addElement(Integer.toString(i));
								}
							}
							// 2월은 28일까지 나옴
						}else if(mm.equals("02")){
							cmodel.addElement("");
							for(int i=1;i<=28;i++){
								if(i<10){
									cmodel.addElement("0"+Integer.toString(i));
								}else{
									cmodel.addElement(Integer.toString(i));
								}
							}
							// 1,3,5,7,8,10,12월은 30일까지 나옴
						}else{
							cmodel.addElement("");
							for(int i=1;i<=31;i++){
								if(i<10){
									cmodel.addElement("0"+Integer.toString(i));
								}else{
									cmodel.addElement(Integer.toString(i));
								}
							}
						}
						//						System.out.println(cmodel.getSize());
						combodd.setModel(cmodel); // 월별에 따른 일수 갱신

					}
					
				}
			});
			comboBox_1.setBounds(126, 10, 69, 24);
		}
		return comboBox_1;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("월");
			label_1.setHorizontalAlignment(SwingConstants.CENTER);
			label_1.setBounds(201, 10, 35, 24);
		}
		return label_1;
	}
	private JComboBox getCombodd() {
		if (combodd == null) {
			combodd = new JComboBox(cmodel);
			combodd.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					

					if(e.getStateChange() == e.SELECTED){ // 콤보박스 값이 선택되었을경우
						dd = (String)(e.getItem());
					}
					
				}
			});
			combodd.setModel(new DefaultComboBoxModel<>(d));
			combodd.setBounds(242, 10, 69, 24);
		}
		return combodd;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("일");
			label_2.setHorizontalAlignment(SwingConstants.CENTER);
			label_2.setBounds(317, 10, 35, 24);
		}
		return label_2;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("적용");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					memberholyeditor();
					
				}
			});
			btnNewButton.setBounds(350, 11, 72, 23);
		}
		return btnNewButton;
	}
}
